import 'package:flutter/material.dart';
import 'package:ost_flutter_module/class/entity/store/store_address_entity.dart';
import 'package:ost_flutter_module/common/adapt.dart';
import 'package:ost_flutter_module/common/dynamic_cell/dynamic_cell_model.dart';
import 'package:ost_flutter_module/common/styles.dart';
import 'package:ost_flutter_module/common/utils.dart';
import 'package:ost_flutter_module/widget/custom_widget/custome_bottom_button.dart';
import 'package:ost_flutter_module/widget/custom_widget/custome_divider.dart';
import 'package:ost_flutter_module/widget/custom_widget/custome_list_tile.dart';
import 'package:ost_flutter_module/widget/custom_widget/custome_textfiled.dart';

class StoreAddressEditPageBody extends StatefulWidget {
  final EditAddress type;
  final StoreAddressEntity entity;
  StoreAddressEditPageBody({Key key, this.entity, this.type}) : super(key: key);

  @override
  _StoreAddressEditPageBodyState createState() =>
      _StoreAddressEditPageBodyState();
}

class _StoreAddressEditPageBodyState extends State<StoreAddressEditPageBody> {
  List<DynamicCellModel> _list = [
    DynamicCellModel(
        type: DynamicCellType.listTileTextFiledInfo,
        info: StoreAddressEdit(title: '收件人', hintText: '请输入姓名')),
    DynamicCellModel(
        type: DynamicCellType.listTileTextFiledInfo,
        info: StoreAddressEdit(title: '联系电话', hintText: '请输入')),
    DynamicCellModel(
        type: DynamicCellType.listTileSelectInfo,
        info: StoreAddressEdit(title: '所在地区', hintText: '请选择')),
    DynamicCellModel(
        type: DynamicCellType.listTileTextFiledInfo,
        info: StoreAddressEdit(title: '详细地址', hintText: '请输入详细地址')),
    DynamicCellModel(
        type: DynamicCellType.listTileCheckInfo,
        info: StoreAddressEdit(title: '设置当前为默认地址')),
  ];

  @override
  void initState() {
    super.initState();

    if (widget.type == EditAddress.edit) {
      _list.map((e) {
        int index = _list.indexOf(e);
        StoreAddressEdit info = e.info;
        if (index == 0) info.descTitle = widget.entity.receiver ?? '';
        if (index == 1) info.descTitle = widget.entity.phone ?? '';
        if (index == 2)
          info.descTitle =
              '${widget.entity.addressProvince} ${widget.entity.addressCity} ${widget.entity.addressDistrict}';
        if (index == 3) info.descTitle = widget.entity.addressDetail ?? '';
        if (index == 4) info.descTitle = '${widget.entity.isDefault}';
      }).toList();
    }
  }

  @override
  Widget build(BuildContext context) {
    return GBottomButton(
      children: [
        ListView.builder(
          itemCount: _list.length,
          itemBuilder: (BuildContext context, int index) {
            DynamicCellModel model = _list[index];
            DynamicCellType type = model.type;
            StoreAddressEdit info = model.info as StoreAddressEdit;

            return Container(
              padding: EdgeInsets.only(top: Adapt.px(Utils.padding / 5)),
              color: Colors.white,
              child: Column(
                children: [
                  GListTile(
                    leadingWidth:
                        type != DynamicCellType.listTileCheckInfo ? 70 : null,
                    leading: Text(info.title, style: TextStyles.titleStyle),
                    trailing: type == DynamicCellType.listTileSelectInfo
                        ? enableText(info.descTitle, hintText: info.hintText)
                        : type == DynamicCellType.listTileTextFiledInfo
                            ? GTextField(
                                key: GlobalKey<GTextFieldState>(),
                                initiaValue: info.descTitle,
                                hintText: info.hintText,
                              )
                            : Container(),
                    enableWidget: type == DynamicCellType.listTileCheckInfo
                        ? (info.descTitle != null && info.descTitle == '1')
                            ? Utils.svgAssets('icon_selected')
                            : Utils.svgAssets('icon_unselected')
                        : null,
                    enable: type == DynamicCellType.listTileSelectInfo ||
                        type == DynamicCellType.listTileCheckInfo,
                  ),
                  SizedBox(height: Adapt.px(Utils.padding / 5)),
                  GDivider(),
                ],
              ),
            );
          },
        ),
      ],
    );
  }

  Widget enableText(String text, {String hintText}) {
    return Text(
      text ?? hintText,
      style: (text != null && text.isNotEmpty)
          ? TextStyles.titleStyle
          : TextStyles.hintTitleStyle,
    );
  }
}

class StoreAddressEdit {
  String title;
  String descTitle;
  String hintText;

  StoreAddressEdit({this.hintText, this.title, this.descTitle});
}
